/*
Auhtor      : Pratibh Prakash
Company     : Fujitsu America
Created Date: 22 Sep-2009
Modified    : 
Description : Class used by Trigger: updateTerritoryNameField for sending waring email stating all the Account Ids 
              for which total length of all Territory names exceeds 255 characters
*/
public class TerritoryWarningEmail {
	private static Set<String> postalCodesForWarning = new Set<String>();//Static Set for starong postal codes for sending mail. Set is populated in Before Insert  and used in After Insert event of trigger.
	
	public static void preparepostalCodesForWarning(Set<String> postalCodesForWarningList){
		postalCodesForWarning.addAll(postalCodesForWarningList);
	}
	public static Set<String> getpostalCodesForWarning(){
		return postalCodesForWarning;
	}
	
	public static void sendWarningEmail(Set<String> accountIds){
        System.debug('*********In Sending Warning Email*********');
        
        Messaging.SingleEmailMessage warningMail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {Label.Admin_Email_Address};
        warningMail.setSenderDisplayName(Label.IM_Sender_Name);
        warningMail.setToAddresses(toAddresses);
        warningMail.setSubject('System Issue: Too Many Territories assigned to an Account');
        warningMail.setBccSender(false);
        String emailbody ='Trigger Warning for the Trigger: <b>updateTerritoryNameField</b>. The following account record(s) is matching multiple Territories based on its Zip Code field.Some of the Territories have not been populated because total length of all Territory names exceeds 255 characters. The territories that were not populated will not be included in the Territory Assignment process.<br></br><br></br>';
        emailbody += '<table><tr><td>OrganizationName</td><td>:</td><td>'+userInfo.getOrganizationName()+'</td></tr>';
        emailbody += '<tr><td>OrginizationId</td><td>:</td><td>'+userInfo.getOrganizationId()+'<td></tr>';
        emailbody += '<tr><td>User Name</td><td>:</td><td>'+userInfo.getName()+'<td></tr>';
        emailbody += '<tr><td>User Id</td><td>:</td><td>'+userInfo.getUserId()+'<td></tr></table><br></br>';
        emailbody += '<table><tr><td><b>Account Ids</b></td></tr>';
        for(String accountId: accountIds){
            emailbody += '<tr><td>'+accountId+'</td></tr>';
        }
        emailbody += '</table>';
        warningMail.setHtmlBody(emailbody);
        Messaging.SendEmailResult[] ers = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { warningMail });
        for(Messaging.SendEmailResult er: ers){
            System.debug('*******Success:  '+er.IsSuccess());
            System.debug('*******Errors:  '+er.getErrors());
        }
    }
}